package elasticsearch;

import org.apache.http.Header;
import org.apache.http.HttpHost;
import org.apache.http.RequestLine;

import org.apache.http.client.config.RequestConfig;
import org.apache.http.util.EntityUtils;
import org.elasticsearch.client.*;

import java.io.IOException;

/**
 * @author LIBOFEI
 * @ClassName ElasticSearchTest
 * @description
 * @date 2020/12/3 11:53
 * @Version 1.0
 **/
public class ElasticSearchTest {

    public static void main(String[] args) throws IOException {


    }

    //默认构造器
    public void defaultBuilder() throws IOException {
        RestClient restClient = RestClient.builder(
                new HttpHost("localhost", 9200, "http")).build();

        Request esRequest = new Request("GET","/libofei_es/user/1");
        esRequest.addParameter("pretty", "true");
        esRequest.setJsonEntity("{\"json\":\"text\"}");
        Response response = restClient.performRequest(esRequest);
        RequestLine requestLine = response.getRequestLine();
        System.out.println("requestLine:" + requestLine);
        HttpHost host = response.getHost();
        System.out.println("host:" + host);
        int statusCode = response.getStatusLine().getStatusCode();
        System.out.println("statusCode:" + statusCode);
        Header[] headers = response.getHeaders();
        System.out.println("headers:" + headers);
        String responseBody = EntityUtils.toString(response.getEntity());
        System.out.println("responseBody:" + responseBody);
        restClient.close();
    }


    //配置请求超时可以通过提供RequestConfigCallback实例来完成，同时通过其构建器构建RestClient
    public void requestCallbackBuilder() throws IOException {
        RestClientBuilder builder = RestClient.builder(
                new HttpHost("localhost", 9200))
                .setRequestConfigCallback(
                        new RestClientBuilder.RequestConfigCallback() {
                            @Override
                            public RequestConfig.Builder customizeRequestConfig(
                                    RequestConfig.Builder requestConfigBuilder) {
                                return requestConfigBuilder
                                        .setConnectTimeout(5000)
                                        .setSocketTimeout(60000);
                            }
                        });
        RestClient restClient = builder.build();
        Request esRequest = new Request("GET","/libofei_es/user/1");
        esRequest.addParameter("pretty", "true");
        esRequest.setJsonEntity("{\"json\":\"text\"}");

        Response response = restClient.performRequest(esRequest);
        RequestLine requestLine = response.getRequestLine();
        System.out.println("requestLine:" + requestLine);
        HttpHost host = response.getHost();
        System.out.println("host:" + host);
        int statusCode = response.getStatusLine().getStatusCode();
        System.out.println("statusCode:" + statusCode);
        Header[] headers = response.getHeaders();
        System.out.println("headers:" + headers);
        String responseBody = EntityUtils.toString(response.getEntity());
        System.out.println("responseBody:" + responseBody);
        restClient.close();
    }
}
